<?php
$SIZE = 10;

$cond_vals = new stdClass();
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'track_id, name';
$pops = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, 't.sold__', 'DESC', 1, $SIZE, $opt)->d;

$cond_vals = new stdClass();
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'track_id, name';
$lates = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, 't.id', 'DESC', 1, $SIZE, $opt)->d;

if (isset($_COOKIE['browsed'])) {
  $_SESSION['browsed'] = json_decode($_COOKIE['browsed']);
}

if (!isset($_SESSION['browsed'])) {
  $_SESSION['browsed'] = array();
}

?>
<style>
table.rightbar {
  width: 189px;
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  margin-left: auto;
  text-align: left;
}
table.rightbar td {
  padding: 3px 5px;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  height: 20px;
  word-wrap: break-word;
  vertical-align: middle;
  overflow: hidden;
}
table.rightbar a {
  color: #666;
}
table.rightbar tr.title td {
  background:url(images/titlebg.gif) no-repeat;
  border-bottom: none;
  padding: 0 5px;
  padding-top: 3px;
  height: 25px;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  vertical-align: top;
}
table.rightbar tr.rowodd td {
  background-color: #eee;
}
table.rightbar tr.roweven td {
  background-color: #fff;
}
.linkbtn {
  border: none;
  background: #fff;
  padding: 0;
  text-decoration: underline;
  cursor: pointer;
}
</style>
<div style="width: 100%; text-align: right;">
  <table class="rightbar" cellpadding=0 cellspacing=0>
    <tr class="title"><td>Popuplar Products</td></tr>
    <?php foreach ($pops as $idx => &$p) { ?>
    <tr class="<?php echo ($idx % 2 == 0) ? 'roweven' : 'rowodd'; ?>"><td><a href="#" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&id=' . $p->track_id; ?>')"><?php echo MiscUtils::getShortName($p->name, 20);?></a></td></tr>
    <?php } ?>
    <?php if (count($pops) == 0) { ?>
    <tr class="roweven"><td>None</td></tr>
    <?php } ?>
  </table>
  <div>&nbsp;</div>
  <table class="rightbar" cellpadding=0 cellspacing=0>
    <tr class="title"><td>Newest Products</td></tr>
    <?php foreach ($lates as $idx => &$p) { ?>
    <tr class="<?php echo ($idx % 2 == 0) ? 'roweven' : 'rowodd'; ?>"><td><a href="#" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&id=' . $p->track_id; ?>')"><?php echo MiscUtils::getShortName($p->name, 20);?></a></td></tr>
    <?php } ?>
    <?php if (count($lates) == 0) { ?>
    <tr class="roweven"><td>None</td></tr>
    <?php } ?>
  </table>
  <div>&nbsp;</div>
  <table class="rightbar" cellpadding=0 cellspacing=0>
    <tr class="title"><td>Browsed Products</td></tr>
    <?php foreach ($_SESSION['browsed'] as $idx => &$p) { ?>
    <tr class="<?php echo ($idx % 2 == 0) ? 'roweven' : 'rowodd'; ?>"><td><a href="#" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&id=' . $p->track_id; ?>')"><?php echo MiscUtils::getShortName($p->name, 20);?></a></td></tr>
    <?php } ?>
    <tr>
      <td style="text-align: right;">
        <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
          <input name="action" value="clear" style="display: none;"></input>
          <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
          <input type='submit' value="Clear All" class="linkbtn"></input>
        </form>
      </td>
    </tr>
  </table>
</div>